import { RootStateOptions } from './state'
import { AbtestParamsKey } from '@/experiment/experiments'
type GetABTestExperiment = (eid:AbtestParamsKey, experiment:string) =>string
type GetABTestExperiments = (isObj:boolean) => string
export interface RootGetters {
  getABTestExperiment: (eid:AbtestParamsKey) =>string
  getABTestExperiments: (isObj?:boolean) => string
}

export default {
  /**
  * 获取对应实验ID的ABTest实验分配变体
  * @param {*} state
  *
  * @param {string} eid 实验ID，来源于 src/config/abExperiments 配置里面的实验对象.A.id
  */
  getABTestExperiment: (state:RootStateOptions):GetABTestExperiment => (eid) => {
    const abTestArr = state.abTestStr.split(',')
    const abTest = abTestArr.find(item => item.split('.')[0] === eid)
    let abTestVariation = 'X'
    if (abTest) {
      abTestVariation = abTest.split('.')[1]
    }
    return abTestVariation
  },
  /**
  * @description 获取所有ABTest 实验及实验分配变体
  */
  getABTestExperiments: (state:RootStateOptions):GetABTestExperiments => (isObj = true) => {
    const abTestArr = state.abTestStr ? state.abTestStr.split(',') : []
    if (isObj) {
      const experiments = abTestArr.map(item => {
        const abTestSplit = item.split('.')
        if (abTestSplit.length >= 2) {
          return {
            experiment_id: abTestSplit[0],
            variation_id: abTestSplit[1]
          }
        }
      })
      return JSON.stringify(experiments)
    } else {
      return JSON.stringify(abTestArr)
    }
  }
}
